﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using LifeInSharePoint.Metro.Utilities;

namespace LifeInSharePoint.Metro.Common.Branding.LifeInSharePoint.Metro
{
    public class SiteFeatureReceiver : SPFeatureReceiver
    {
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPSite siteCollection = (SPSite)properties.Feature.Parent;
            SPFeatureProperty masterUrlProperty = properties.Feature.Properties["MasterPage"];
            string masterUrl = masterUrlProperty.Value;

            SPFeatureProperty searchUrlProperty = properties.Feature.Properties["SearchMasterPage"];
            string searchMasterUrl = searchUrlProperty.Value;

            if (String.IsNullOrEmpty(masterUrl) == false)
            {
                masterUrl = SPUrlUtility.CombineUrl(siteCollection.ServerRelativeUrl,
                    "_catalogs/masterpage/" + masterUrl);

                searchMasterUrl = SPUrlUtility.CombineUrl(siteCollection.ServerRelativeUrl,
                    "_catalogs/masterpage/" + searchMasterUrl);

                foreach (SPWeb web in siteCollection.AllWebs)
                {
                    if (web.WebTemplate == "SRCHCENTERLITE" || web.WebTemplate == "SRCHCEN" || web.WebTemplate == "SRCHCENTERFAST")
                    {
                        web.MasterUrl = masterUrl;
                        web.CustomMasterUrl = searchMasterUrl;
                    }

                    else
                    {
                        web.MasterUrl = masterUrl;
                        web.CustomMasterUrl = masterUrl;
                    }
                    web.Update();
                }
            }
        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPSite siteCollection = (SPSite)properties.Feature.Parent;
            string masterUrl = SPUrlUtility.CombineUrl(siteCollection.ServerRelativeUrl,
                "_catalogs/masterpage/v4.master");


            string searchMasterUrl = SPUrlUtility.CombineUrl(siteCollection.ServerRelativeUrl,
                "_catalogs/masterpage/minimal.master");
            //Get the relative path


            foreach (SPWeb web in siteCollection.AllWebs)
            {
                if (web.WebTemplate == "SRCHCENTERLITE" || web.WebTemplate == "SRCHCEN" || web.WebTemplate == "SRCHCENTERFAST")
                {
                    web.CustomMasterUrl = searchMasterUrl;
                }

                else
                {
                    web.MasterUrl = masterUrl;
                    web.CustomMasterUrl = masterUrl;
                }
                web.Update();
            }


            //Delete List
            SPListCollection lists = siteCollection.RootWeb.Lists;
            SPList list = lists["Metro UI Themes"];
            lists.Delete(list.ID);
        }
    }
}
